//
//  RetweetView.swift
//  weibobbaj
//
//  Created by Tiffany宝 on 16/4/21.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

import UIKit
import SnapKit
import YYText

class RetweetView: UIView {
    
    var bottomConstraint: Constraint?
    
    var statusViewModel: CellStatusViewModel?{
        
        didSet{
            
            //            if let t = statusViewModel?.status?.retweeted_status?.text, n = statusViewModel?.status?.retweeted_status?.user?.name{
            //
            //                contentLabel.text = "@\(n)~\(t)"
            //            }
            ////            contentLabel.text = statusViewModel?.status?.retweeted_status?.text
            
            //逻辑判断放在 CellStatusViewModel 中
//            contentLabel.text = statusViewModel?.retweetText
            contentLabel.attributedText = statusViewModel?.retweetAttributedString
            
            //约束失效
            bottomConstraint?.uninstall()
            
            // ***** 转发微博的配图 ******
            if let pictures = statusViewModel?.status?.retweeted_status?.pic_urls where pictures.count > 0{
                
//                print("有配图\(pictures.count)张")
                //有配图,就更新配图的大小(设置宽高)
                
                //判断是否为单图,修改 item 的 size
//                if pictures.count == 1{
//                    
//                    let retweetPic = retweetPicView.collectionViewLayout as! UICollectionViewFlowLayout
//                    retweetPic.itemSize = statusViewModel!.retweetPicViewSize
//                }else{
//                    
//                    let retweetPic = retweetPicView.collectionViewLayout as! UICollectionViewFlowLayout
//                    retweetPic.itemSize = CGSizeMake(itemWH, itemWH)
//                }
//                
//                retweetPicView.snp_updateConstraints(closure: { (make) -> Void in
//                    
//                    make.size.equalTo(statusViewModel!.retweetPicViewSize)
//                })
//                
//                //设置数据 (根据数据计算出配图视图的大小并设置上去)
//                retweetPicView.pictures = pictures
                
                
                //元组一句话搞定
                retweetPicView.dataInfo = (pictures,statusViewModel!.retweetPicViewSize)
                
                //显示配图
                retweetPicView.hidden = false
                
                //更新约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
//                                        make.height.equalTo(100)
                    
                    bottomConstraint = make.bottom.equalTo(retweetPicView).offset(StatusCellMargin).constraint
                })
                
            }else{
                
                //没有配图
                
                //隐藏配图
                retweetPicView.hidden = true
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
//                                        make.height.equalTo(0)
                    bottomConstraint =  make.bottom.equalTo(contentLabel).offset(StatusCellMargin).constraint
                    
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        //        backgroundColor = RandomColor
        
        //添加控件
        addSubview(contentLabel)
        addSubview(retweetPicView)
        
        //添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.equalTo(self).offset(StatusCellMargin)
        }
        
        //转发微博配图约束
        retweetPicView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            //假定size
            make.size.equalTo(CGSize(width: 100, height: 100))
            
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            //记录一下底部约束
            bottomConstraint = make.bottom.equalTo(retweetPicView).offset(StatusCellMargin).constraint
        }
    }
    
    //懒加载转发微博内容label
//    private lazy var contentLabel: UILabel = {
//        
//        let content = UILabel(textFont: StatusCellContentFontSize, textColor: UIColor.darkGrayColor(), preferredMaxLayoutWidth: SCREENW - 2 * StatusCellMargin)
//        
//        content.text = "转发微博内容label转发微博内容label转发微博内容label转发微博内容label转发微博内容label转发微博内容label转发微博内容label转发微博内容label转发微博内容label转发微博内容label转发微博内容label"
//        
//        return content
//    }()
    private lazy var contentLabel: YYLabel = {
        // UILabel(textColor: UIColor.darkGrayColor(), fontSize: HMStatusCellContentFontSize, preferredMaxLayoutWidth: SCREENW - 2 * HMStatusCellMargin)
        let label = YYLabel()
        label.userInteractionEnabled = true
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(StatusCellContentFontSize)
        label.preferredMaxLayoutWidth = SCREENW - 2 * StatusCellMargin
        label.numberOfLines = 0
        return label
    }()
    
    //******* UICollectionViewFlowLayout 而不是 UICollectionViewLayout ********
//    private lazy var retweetPicView: RetweetPictureView = RetweetPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewLayout())
    
    private lazy var retweetPicView: RetweetPictureView = RetweetPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    
    
}
